package com.unsins.test.coherence;

/**
 * Created by IntelliJ IDEA.
 * User: odpsoft
 * Date: 2009-4-24
 * Time: 7:51:49
 */

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
import com.tangosol.net.cache.ContinuousQueryCache;
import com.tangosol.util.Filter;
import com.tangosol.util.extractor.IdentityExtractor;
import com.tangosol.util.filter.LikeFilter;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class DatabaseCache {
    NamedCache cache;

    public DatabaseCache() {
    }

    public void createCache() {
        cache = CacheFactory.getCache("DBBackedCache");

    }

    public void addEntry() {
        cache.put(new String("catalog3"), new String("Evolving Grid Management"));
    }

    public void retrieveEntry() {
        System.out.println((String) cache.get("catalog1"));
    }

    public void eraseEntry() {
        cache.remove(new String("catalog3"));
    }

    public void queryCache() {
        Filter filter = new LikeFilter(IdentityExtractor.INSTANCE, "Tuning%", '\\', true);
        HashSet hashSet = new HashSet();
        hashSet.add(new String("catalog1"));
        hashSet.add(new String("catalog2"));
        hashSet.add(new String("catalog3"));

        Map map = cache.getAll(hashSet);
        ContinuousQueryCache queryCache = new ContinuousQueryCache(cache, filter);
        Set results = queryCache.entrySet(filter);
        /* Set results = cache.entrySet(filter);*/

        if (results.isEmpty())
            System.out.println("Result Set Empty");
        for (Iterator i = results.iterator(); i.hasNext();) {
            Map.Entry e = (Map.Entry) i.next();
            System.out.println("Catalog ID: " + e.getKey() + ", Title: " + e.getValue());
        }
    }


    public static void main(String[] args) {
        DatabaseCache databaseCache = new DatabaseCache();
       databaseCache.createCache();
        //databaseCache.addEntry();
        databaseCache.retrieveEntry();
        //databaseCache.eraseEntry();
        databaseCache.queryCache();
    }
}

